package com.example.demo.controller;

import com.example.demo.DTO.CartGoodsSellerInfo;
import com.example.demo.DTO.StoreDTO;
import com.example.demo.pojo.Cart;
import com.example.demo.pojo.Result;
import com.example.demo.request.SelectionStatusRequest;
import com.example.demo.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Validated
public class CartController {

	@Autowired
	private ICartService cartService;

	@GetMapping("/getSelected/{userID}")
	public Result getSelectedCartItems(@PathVariable Integer userID) {
		return Result.success(cartService.getSelectedCartItemsByUser(userID));
	}
	@PostMapping("/add")
	public Cart addToCart(@RequestBody Cart cart) {
		return cartService.addToCart(cart);
	}

	@GetMapping("/goodsAndSellers/{userID}")
	public CartGoodsSellerInfo findGoodsAndSellersByUserID(@PathVariable Integer userID) {
		return cartService.findGoodsAndSellersByUserID(userID);
	}
	@PostMapping("/updateSelectionStatus")
	public Result updateSelectionStatus(@RequestBody SelectionStatusRequest request) {
		if (request.getSellerID() != null && request.getUserID() != null) {
			cartService.updateStoreSelectionStatus(request.getUserID(), request.getSellerID(), request.getSelected());
			return Result.success("Store selection status updated successfully");
		} else if (request.getGoodsID() != null && request.getUserID() != null) {
			cartService.updateCartItemSelectionStatus(request.getUserID(), request.getGoodsID(), request.getSelected());
			return Result.success("Item selection status updated successfully");
		} else {
			return Result.error("Invalid request");
		}
	}
	@DeleteMapping("/{cartID}")
	public void removeCartItem(@PathVariable Integer cartID) {
		cartService.removeCartItem(cartID);
	}
}
